﻿using System.Linq;
using System.Web.Mvc;

using SimpleForum.Utils;
using SimpleForum.Data.Models;
using SimpleForum.Data.Services;
using SimpleForum.Data.Models.ViewModels;

namespace SimpleForum.Controllers
{
    public class ThreadController : BaseController
    {
        private readonly IForumService ForumService;
        private readonly IThreadService ThreadService;
        private readonly IPostService PostService;
        private readonly ITagService TagService;

        public ThreadController(IForumService ForumService, IThreadService ThreadService, IPostService PostService, ITagService TagService)
        {
            this.ForumService = ForumService;
            this.ThreadService = ThreadService;
            this.PostService = PostService;
            this.TagService = TagService;
        }

        public ActionResult View(int id, string name)
        {
            var Thread = ThreadService.GetThread(id);

            if (Thread != null)
            {
                ThreadService.ThreadViewed(Thread);

                return View(Thread);
            }
            else return EntityNotFound();
        }

        [Authorize]
        public ActionResult Create(int id)
        {
            var Forum = ForumService.GetForum(id);

            if (Forum != null)
            {
                return View(
                    new ThreadViewModel
                    {
                        Forum = Forum
                    }
                );
            }
            else return EntityNotFound();
        }

        [HttpPost, Authorize]
        public ActionResult Create(ThreadViewModel Thread)
        {
            if (ModelState.IsValid)
            {
                var CreatedThread = ThreadService.CreateThread(Thread.Forum.Id, Identity.User.Id, Thread.Title, Thread.Message, Thread.Tags);

                return RedirectToAction("view", "thread", new { id = CreatedThread.Id, name = CreatedThread.Name.Slugify() });
            }

            return View(Thread);
        }

        [Authorize]
        public ActionResult Reply(int id)
        {
            var Thread = ThreadService.GetThread(id);

            if (Thread != null)
            {
                return View(
                    new ThreadReplyViewModel()
                    {
                        Thread = Thread,
                        Post = new Post() { ThreadId = id }
                    }
                );
            }
            else return EntityNotFound();
        }

        [HttpPost, Authorize]
        public ActionResult Reply(Post Post)
        {
            if (ModelState.IsValid)
            {
                var CreatedPost = PostService.CreatePost(Post.ThreadId, Identity.User.Id, Post.Body);

                return RedirectToAction("view", "thread", new { id = Post.ThreadId, name = CreatedPost.Thread.Name.Slugify() });
            }
            else return EntityNotFound();
        }

        [Classes.Attributes.Ajax]
        public JsonResult GetTags()
        {
            return Json(
                TagService.GetTags().GroupBy(x => x.Name).Select(x => x.Key),
                "text/plain",
                JsonRequestBehavior.AllowGet
            );
        }
    }
}